package robotleMvc.ui.lizhi.fensan
{
	import _Pan3D.display3D.Display3DSprite;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Matrix3D;
	
	public class FensanDisplaySprite extends Display3DSprite
	{
		protected var lizhiVertexBuffer:VertexBuffer3D;
		protected var lizhiStateVertexBuffer:VertexBuffer3D;
		protected var lizhiIndexBuffer:IndexBuffer3D;
		protected var _program3D:Program3D;
		protected var _timer:Number=0;
		public function FensanDisplaySprite(context:Context3D)
		{
			super(context);
		}
		override public function update() : void {
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				if(_program3D){
					_context.setProgram(_program3D);
				}else{
					_context.setProgram(this.program);
				}
				setVc();
				setVa();
				resetVa();
			}
		}
		protected function initLizhiData():void
		{
			var context3D:Context3D=_context;
			var _triNum:Number=360/2;
			_triNum=100
			var _pArr:Array=new Array;
			var _mArr:Array=new Array;
			var _iArr:Array=new Array;
			var _w:Number=50;
			var _h:Number=10;
			for(var i:int=0;i<_triNum;i++){
				var zz:Number=0;
				_pArr.push(0, _h, zz, 0,0, 1);
				_pArr.push(_w*2, _h, zz, 1,0, 1);
				_pArr.push(_w*2, -_h, zz, 1,1, 1);
				_pArr.push(0, -_h, zz, 0,1, 1);
				var a:Number=0;
				var b:Number= Math.random()*360*Math.PI/180;
				var c:Number= Math.random()*360*Math.PI/180;
				_mArr.push(a,b,c);
				_mArr.push(a,b,c);
				_mArr.push(a,b,c);
				_mArr.push(a,b,c);
				var n:int=i*4;
				_iArr.push(n+0,n+1,n+2);
				_iArr.push(n+0,n+2,n+3);
			}
			lizhiVertexBuffer  = context3D.createVertexBuffer( _triNum*4, 6);
			lizhiVertexBuffer.uploadFromVector(Vector.<Number>(_pArr), 0, _pArr.length/6);
			lizhiStateVertexBuffer  = context3D.createVertexBuffer( _triNum*4, 3);
			lizhiStateVertexBuffer.uploadFromVector(Vector.<Number>(_mArr), 0, _mArr.length/3);
			lizhiIndexBuffer  = context3D.createIndexBuffer(_iArr.length);
			lizhiIndexBuffer.uploadFromVector(Vector.<uint>(_iArr), 0, _iArr.length);
		}
		override protected function setVa() : void {
			if(!lizhiIndexBuffer){
				initLizhiData();
				return;
			}
			_context.setVertexBufferAt(0,lizhiVertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1,lizhiVertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(2,lizhiStateVertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(lizhiIndexBuffer, 0, -1);
		}
		public function clearOldBuff():void //将模型清空
		{
			if(!lizhiIndexBuffer)return;
			lizhiIndexBuffer.dispose();
			lizhiStateVertexBuffer.dispose();
			lizhiVertexBuffer.dispose();
			lizhiIndexBuffer=null;
			lizhiStateVertexBuffer=null;
			lizhiVertexBuffer=null;
		}
	
		
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
		}
		
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			var vectorData : Vector.<Number>=new Vector.<Number>;
			_timer=_timer+1;
			
			
			var t:Number=_timer%100;
			var b:Number=0;
			var c:Number=0;
			var d:Number=0;
	
			vectorData.push(t, 1, 0, 0,b, 0, 0, 0, c, 0,0, 0, d, 0, 0, 0);
			var dataMatrix : Matrix3D = new Matrix3D(vectorData);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 8, dataMatrix, true);  //这里暂可以传一个时间进去;
		}

		public function get program3D():Program3D
		{
			return _program3D;
		}

		public function set program3D(value:Program3D):void
		{
			_program3D = value;
		}

	}
}